home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gpp-1_42.lha / g++-1.42.0 / cplus-cvt.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  56KB  |  1,845 lines

  1. /* Language-level data type conversion for GNU C++.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@mcc.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This file contains the functions for converting C expressions
  23.    to different data types.  The only entry point is `convert'.
  24.    Every language front end must have a `convert' function
  25.    but what kind of conversions it does will depend on the language.  */
  26.  
  27. #include "config.h"
  28. #include "tree.h"
  29. #include "cplus-tree.h"
  30. #include "assert.h"
  31.  
  32. #define NULL 0
  33.  
  34. /* Change of width--truncation and extension of integers or reals--
  35.    is represented with NOP_EXPR.  Proper functioning of many things
  36.    assumes that no other conversions can be NOP_EXPRs.
  37.  
  38.    Conversion between integer and pointer is represented with CONVERT_EXPR.
  39.    Converting integer to real uses FLOAT_EXPR
  40.    and real to integer uses FIX_TRUNC_EXPR.
  41.  
  42.    Here is a list of all the functions that assume that widening and
  43.    narrowing is always done with a NOP_EXPR:
  44.      In c-convert.c, convert_to_integer.
  45.      In c-typeck.c, build_binary_op_nodefault (boolean ops),
  46.         and truthvalue_conversion.
  47.      In expr.c: expand_expr, for operands of a MULT_EXPR.
  48.      In fold-const.c: fold.
  49.      In tree.c: get_narrower and get_unwidened.
  50.  
  51.    C++: in multiple-inheritance, converting between pointers may involve
  52.    adjusting them by a delta stored within the class definition.  */
  53.  
  54. /* Subroutines of `convert'.  */
  55.  
  56. static tree
  57. convert_to_pointer (type, expr)
  58.      tree type, expr;
  59. {
  60.   register tree intype = TREE_TYPE (expr);
  61.   register enum tree_code form = TREE_CODE (intype);
  62.   
  63.   if (integer_zerop (expr))
  64.     {
  65.       if (type == TREE_TYPE (null_pointer_node))
  66.     return null_pointer_node;
  67.       expr = build_int_2 (0, 0);
  68.       TREE_TYPE (expr) = type;
  69.       return expr;
  70.     }
  71.  
  72.   if (form == POINTER_TYPE)
  73.     {
  74.       intype = TYPE_MAIN_VARIANT (intype);
  75.  
  76.       if (TYPE_MAIN_VARIANT (type) != intype
  77.       && IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype)))
  78.     {
  79.       enum tree_code code = PLUS_EXPR;
  80.       tree basetype = get_base_type (TREE_TYPE (TYPE_MAIN_VARIANT (type)),
  81.                      TREE_TYPE (intype), 1);
  82.       if (basetype == error_mark_node)
  83.         return error_mark_node;
  84.       if (basetype == NULL_TREE)
  85.         {
  86.           basetype = get_base_type (TREE_TYPE (intype),
  87.                     TREE_TYPE (TYPE_MAIN_VARIANT (type)), 1);
  88.           if (basetype == error_mark_node)
  89.         return error_mark_node;
  90.           code = MINUS_EXPR;
  91.         }
  92.       if (basetype)
  93.         {
  94.           if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
  95.           || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
  96.           || DECL_OFFSET (TYPE_NAME (basetype)) != 0)
  97.         {
  98.           /* Need to get the path we took.  */
  99.           tree path;
  100.  
  101.           if (code == PLUS_EXPR)
  102.             get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
  103.           else
  104.             get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
  105.           return build_vbase_path (code, type, expr, path, 0);
  106.         }
  107.         }
  108.     }
  109.       return build1 (NOP_EXPR, type, expr);
  110.     }
  111.  
  112.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  113.     {
  114.       if (type_precision (intype) == POINTER_SIZE)
  115.     return build1 (CONVERT_EXPR, type, expr);
  116.       return convert_to_pointer (type,
  117.                  convert (type_for_size (POINTER_SIZE, 0),
  118.                       expr));
  119.     }
  120.  
  121.   assert (form != OFFSET_TYPE);
  122.  
  123.   if (IS_AGGR_TYPE (intype))
  124.     {
  125.       /* If we cannot convert to the specific pointer type,
  126.      try to convert to the type `void *'.  */
  127.       tree rval;
  128.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  129.       if (rval)
  130.     {
  131.       if (rval == error_mark_node)
  132.         error ("ambiguous pointer conversion");
  133.       return rval;
  134.     }
  135.     }
  136.  
  137.   error ("cannot convert to a pointer type");
  138.  
  139.   return null_pointer_node;
  140. }
  141.  
  142. /* Like convert, except permit conversions to take place which
  143.    are not normally allowed due to visibility restrictions
  144.    (such as conversion from sub-type to private super-type).  */
  145. static tree
  146. convert_to_pointer_force (type, expr)
  147.      tree type, expr;
  148. {
  149.   register tree intype = TREE_TYPE (expr);
  150.   register enum tree_code form = TREE_CODE (intype);
  151.   
  152.   if (integer_zerop (expr))
  153.     {
  154.       if (type == TREE_TYPE (null_pointer_node))
  155.     return null_pointer_node;
  156.       expr = build_int_2 (0, 0);
  157.       TREE_TYPE (expr) = type;
  158.       return expr;
  159.     }
  160.  
  161.   if (form == POINTER_TYPE)
  162.     {
  163.       intype = TYPE_MAIN_VARIANT (intype);
  164.  
  165.       if (TYPE_MAIN_VARIANT (type) != intype
  166.       && IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype)))
  167.     {
  168.       enum tree_code code = PLUS_EXPR;
  169.       tree path, basetype;
  170.       int distance = get_base_distance (TREE_TYPE (type),
  171.                         TYPE_MAIN_VARIANT (TREE_TYPE (intype)), 0, &path);
  172.       if (distance == -2)
  173.         {
  174.         ambig:
  175.           error_with_aggr_type (TREE_TYPE (type), "type `%s' is ambiguous baseclass of `%s'",
  176.                     TYPE_NAME_STRING (TREE_TYPE (intype)));
  177.           return error_mark_node;
  178.         }
  179.       if (distance == -1)
  180.         {
  181.           distance = get_base_distance (TREE_TYPE (intype),
  182.                         TYPE_MAIN_VARIANT (TREE_TYPE (type)), 0, &path);
  183.           if (distance == -2)
  184.         goto ambig;
  185.           if (distance < 0)
  186.         /* Doesn't need any special help from us.  */
  187.         return build1 (NOP_EXPR, type, expr);
  188.  
  189.           code = MINUS_EXPR;
  190.         }
  191.       return build_vbase_path (code, type, expr, path, 0);
  192.     }
  193.       return build1 (NOP_EXPR, type, expr);
  194.     }
  195.  
  196.   return convert_to_pointer (type, expr);
  197. }
  198.  
  199. /* We are passing something to a function which requires a reference.
  200.    The type we are interested in is in TYPE. The initial
  201.    value we have to begin with is in ARG.
  202.  
  203.    FLAGS controls how we manage visibility checking.  */
  204. static tree
  205. build_up_reference (type, arg, flags)
  206.      tree type, arg;
  207.      int flags;
  208. {
  209.   tree rval, targ;
  210.   int literal_flag = 0;
  211.   tree argtype = TREE_TYPE (arg), basetype = argtype;
  212.   tree target_type = TREE_TYPE (type);
  213.  
  214.   assert (TREE_CODE (type) == REFERENCE_TYPE);
  215.   if (flags != 0
  216.       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
  217.       && IS_AGGR_TYPE (argtype)
  218.       && IS_AGGR_TYPE (target_type))
  219.     {
  220.       basetype = get_base_type (target_type, TYPE_MAIN_VARIANT (argtype),
  221.                 (flags & LOOKUP_PROTECTED_OK) ? 3 : 2);
  222.       if ((flags & LOOKUP_PROTECT) && basetype == error_mark_node)
  223.     return error_mark_node;
  224.       if (basetype == NULL_TREE)
  225.     {
  226.       error_not_base_type (target_type, argtype);
  227.       return error_mark_node;
  228.     }
  229.     }
  230.  
  231.   targ = arg;
  232.   if (TREE_CODE (targ) == SAVE_EXPR)
  233.     targ = TREE_OPERAND (targ, 0);
  234.  
  235.   switch (TREE_CODE (targ))
  236.     {
  237.     case INDIRECT_REF:
  238.       /* This is a call to a constructor which did not know what it was
  239.      initializing until now: it needs to initialize a temporary.  */
  240.       if (TYPE_HAS_CONSTRUCTOR (arg))
  241.     {
  242.       tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1);
  243.       TYPE_HAS_CONSTRUCTOR (targ) = 0;
  244.       return build_up_reference (type, temp, flags);
  245.     }
  246.       /* Let &* cancel out to simplify resulting code.
  247.          Also, throw away intervening NOP_EXPRs.  */
  248.       arg = TREE_OPERAND (targ, 0);
  249.       if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == REFERENCE_EXPR)
  250.     arg = TREE_OPERAND (arg, 0);
  251.  
  252.       rval = build1 (REFERENCE_EXPR, type, arg);
  253.       literal_flag = TREE_LITERAL (arg);
  254.       goto done;
  255.  
  256.       /* Get this out of a register if we happened to be in one by accident.
  257.      Also, build up references to non-lvalues it we must.  */
  258.       /* For &x[y], return (&) x+y */
  259.     case ARRAY_REF:
  260.       if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
  261.     return error_mark_node;
  262.       rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
  263.                   TREE_OPERAND (targ, 1));
  264.       TREE_TYPE (rval) = type;
  265.       if (TREE_LITERAL (TREE_OPERAND (targ, 1))
  266.       && staticp (TREE_OPERAND (targ, 0)))
  267.     TREE_LITERAL (rval) = 1;
  268.       return rval;
  269.  
  270.     case SCOPE_REF:
  271.       /* Could be a reference to a static member.  */
  272.       {
  273.     tree field = TREE_OPERAND (targ, 1);
  274.     if (TREE_STATIC (field))
  275.       {
  276.         rval = build1 (ADDR_EXPR, type, field);
  277.         literal_flag = 1;
  278.         goto done;
  279.       }
  280.       }
  281.       /* we should have farmed out member pointers above.  */
  282.       assert (0);
  283.  
  284.     case COMPONENT_REF:
  285.       rval = build_component_addr (targ, build_pointer_type (argtype),
  286.                    "attempt to make a reference to bit-field structure member `%s'");
  287.       TREE_TYPE (rval) = type;
  288.       literal_flag = staticp (TREE_OPERAND (targ, 0));
  289. #if 0
  290.       goto done_but_maybe_warn;
  291. #else
  292.       goto done;
  293. #endif
  294.  
  295.       /* Anything not already handled and not a true memory reference
  296.      needs to have a reference built up.  Do so silently for
  297.      things like integers and return values from function,
  298.      but complain if we need a reference to something declared
  299.      as `register'.  */
  300.  
  301.     case RESULT_DECL:
  302.       if (staticp (targ))
  303.     literal_flag = 1;
  304.       TREE_ADDRESSABLE (targ) = 1;
  305.       put_var_into_stack (targ);
  306.       break;
  307.  
  308.     case PARM_DECL:
  309.       if (targ == current_class_decl)
  310.     {
  311.       error ("address of `this' not available");
  312.       TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
  313.       put_var_into_stack (targ);
  314.       break;
  315.     }
  316.       /* Fall through.  */
  317.     case VAR_DECL:
  318.     case CONST_DECL:
  319.       if (TREE_REGDECL (targ) && !TREE_ADDRESSABLE (targ))
  320.     warning ("address needed to build reference for `%s', which is declared `register'",
  321.          IDENTIFIER_POINTER (DECL_NAME (targ)));
  322.       else if (staticp (targ))
  323.     literal_flag = 1;
  324.  
  325.       TREE_ADDRESSABLE (targ) = 1;
  326.       put_var_into_stack (targ);
  327.       break;
  328.  
  329.     case COMPOUND_EXPR:
  330.       {
  331.     tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
  332.                           LOOKUP_PROTECT);
  333.     rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
  334.     TREE_LITERAL (rval) = staticp (TREE_OPERAND (targ, 1));
  335.     return rval;
  336.       }
  337.  
  338.     case MODIFY_EXPR:
  339.     case INIT_EXPR:
  340.       {
  341.     tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
  342.                           LOOKUP_PROTECT);
  343.     rval = build (COMPOUND_EXPR, type, arg, real_reference);
  344.     TREE_LITERAL (rval) = staticp (TREE_OPERAND (targ, 0));
  345.     return rval;
  346.       }
  347.  
  348.     case COND_EXPR:
  349.       return build (COND_EXPR, type,
  350.             TREE_OPERAND (targ, 0),
  351.             build_up_reference (type, TREE_OPERAND (targ, 1), LOOKUP_PROTECT),
  352.             build_up_reference (type, TREE_OPERAND (targ, 2), LOOKUP_PROTECT));
  353.  
  354.     case WITH_CLEANUP_EXPR:
  355.       return build (WITH_CLEANUP_EXPR, type,
  356.             build_up_reference (type, TREE_OPERAND (targ, 0), LOOKUP_PROTECT),
  357.             0, TREE_OPERAND (targ, 2));
  358.  
  359.     default:
  360.       break;
  361.     }
  362.  
  363.   if (TREE_ADDRESSABLE (targ) == 0)
  364.     {
  365.       tree temp;
  366.  
  367.       if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
  368.     {
  369.       temp = build_cplus_new (argtype, targ, 1);
  370.       rval = build1 (ADDR_EXPR, type, temp);
  371.       goto done;
  372.     }
  373.       else
  374.     {
  375.       temp = get_temp_name (argtype, 0);
  376.       if (global_bindings_p ())
  377.         {
  378.           /* Give this new temp some rtl and initialize it.  */
  379.           DECL_INITIAL (temp) = targ;
  380.           TREE_STATIC (temp) = 1;
  381.           finish_decl (temp, targ, NULL_TREE);
  382.           /* Do this after declaring it static.  */
  383.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  384.           literal_flag = TREE_LITERAL (rval);
  385.           goto done;
  386.         }
  387.       else
  388.         {
  389.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  390.           /* Put a value into the rtl.  */
  391.           if (IS_AGGR_TYPE (argtype))
  392.         {
  393.           /* This may produce surprising results,
  394.              since we commit to initializing the temp
  395.              when the temp may not actually get used.  */
  396.           expand_aggr_init (temp, targ, 0);
  397.           TREE_TYPE (rval) = type;
  398.           literal_flag = TREE_LITERAL (rval);
  399.           goto done;
  400.         }
  401.           else
  402.         {
  403.           if (basetype != argtype)
  404.             rval = convert_pointer_to (target_type, rval);
  405.           else
  406.             TREE_TYPE (rval) = type;
  407.           return build (COMPOUND_EXPR, type,
  408.                 build (MODIFY_EXPR, argtype, temp, arg), rval);
  409.         }
  410.         }
  411.     }
  412.     }
  413.   else
  414.     {
  415.       if (TREE_CODE (arg) == SAVE_EXPR)
  416.     abort ();
  417.       rval = build1 (ADDR_EXPR, type, arg);
  418.     }
  419.  
  420.  done_but_maybe_warn:
  421.   if (TREE_READONLY (arg)
  422.       && ! TREE_READONLY (target_type))
  423.     readonly_warning_or_error (arg, "conversion to reference");
  424.  
  425.  done:
  426.   if (TYPE_LANG_SPECIFIC (argtype)
  427.       && (TYPE_USES_MULTIPLE_INHERITANCE (argtype)
  428.       || TYPE_USES_VIRTUAL_BASECLASSES (argtype)))
  429.     {
  430.       TREE_TYPE (rval) = TYPE_POINTER_TO (argtype);
  431.       rval = convert_pointer_to (target_type, rval);
  432.       TREE_TYPE (rval) = type;
  433.     }
  434.   TREE_LITERAL (rval) = literal_flag;
  435.   return rval;
  436. }
  437.  
  438. /* For C++: Only need to do one-level references, but cannot
  439.    get tripped up on signed/unsigned differences.
  440.  
  441.    If DECL is NULL_TREE it means convert as though casting (by force).
  442.    If it is ERROR_MARK_NODE, it means the conversion is implicit,
  443.    and that temporaries may be created.
  444.    Otherwise, DECL is a _DECL node which can be used in error reporting.  */
  445. tree
  446. convert_to_reference (decl, reftype, expr, strict, flags)
  447.      tree decl;
  448.      tree reftype, expr;
  449.      int strict, flags;
  450. {
  451.   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
  452.   register tree intype = TREE_TYPE (expr);
  453.   register enum tree_code form = TREE_CODE (intype);
  454.  
  455.   assert (TREE_CODE (reftype) == REFERENCE_TYPE);
  456.  
  457.   if (form == REFERENCE_TYPE)
  458.     intype = TREE_TYPE (intype);
  459.   intype = TYPE_MAIN_VARIANT (intype);
  460.  
  461.   /* @@ Probably need to have a check for X(X&) here.  */
  462.  
  463.   if (IS_AGGR_TYPE (intype))
  464.     {
  465.       tree rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
  466.       if (rval)
  467.     {
  468.       if (rval == error_mark_node)
  469.         error ("ambiguous pointer conversion");
  470.       return rval;
  471.     }
  472.       else if (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1))
  473.     {
  474.       if (TYPE_NEEDS_DESTRUCTOR (type))
  475.         {
  476.           rval = cleanup_after_call (rval);
  477.           rval = convert_to_reference (NULL_TREE, reftype, rval, strict, flags);
  478.         }
  479.       else
  480.         {
  481.           decl = get_temp_name (type, 0);
  482.           rval = build (INIT_EXPR, type, decl, rval);
  483.           rval = build (COMPOUND_EXPR, reftype, rval,
  484.                 convert_to_reference (NULL_TREE, reftype, decl,
  485.                           strict, flags));
  486.         }
  487.       return rval;
  488.     }
  489.  
  490.       if (form == REFERENCE_TYPE
  491.       && type != intype
  492.       && TYPE_LANG_SPECIFIC (intype)
  493.       && (TYPE_USES_VIRTUAL_BASECLASSES (intype)
  494.           || TYPE_USES_MULTIPLE_INHERITANCE (intype)))
  495.     {
  496.       /* If it may move around, build a fresh reference.  */
  497.       expr = convert_from_reference (expr);
  498.       form = TREE_CODE (TREE_TYPE (expr));
  499.     }
  500.     }
  501.  
  502.   /* @@ Perhaps this should try to go through a constructor first
  503.      @@ for proper initialization, but I am not sure when that
  504.      @@ is needed or desirable.
  505.  
  506.      @@ The second disjunct is provided to make references behave
  507.      @@ as some people think they should, i.e., an interconvertability
  508.      @@ between references to builtin types (such as short and
  509.      @@ unsigned short).  There should be no conversion between
  510.      @@ types whose codes are different, or whose sizes are different.  */
  511.  
  512.   if (((IS_AGGR_TYPE (type) || IS_AGGR_TYPE (intype))
  513.        && comptypes (type, intype, strict))
  514.       || (!IS_AGGR_TYPE (type)
  515.       && TREE_CODE (type) == TREE_CODE (intype)
  516.       && int_size_in_bytes (type) == int_size_in_bytes (intype)))
  517.     {
  518.       /* If EXPR is of aggregate type, and is really a CALL_EXPR,
  519.      then we don't need to convert it to reference type if
  520.      it is only being used to initialize DECL which is also
  521.      of the same aggregate type.  */
  522.       if (form == REFERENCE_TYPE
  523.       || (decl != NULL_TREE && decl != error_mark_node
  524.           && IS_AGGR_TYPE (type)
  525.           && TREE_CODE (expr) == CALL_EXPR
  526.           && TYPE_MAIN_VARIANT (type) == intype))
  527.     {
  528.       if (decl && decl != error_mark_node)
  529.         {
  530.           tree e1 = build (INIT_EXPR, void_type_node, decl, expr);
  531.           tree e2;
  532.  
  533.           TREE_VOLATILE (e1) = 1;
  534.           if (form == REFERENCE_TYPE)
  535.         e2 = build1 (NOP_EXPR, reftype, decl);
  536.           else
  537.         {
  538.           e2 = build_unary_op (ADDR_EXPR, decl, 0);
  539.           e2 = build1 (REFERENCE_EXPR, reftype, e2);
  540.         }
  541.           return build_compound_expr (tree_cons (NULL_TREE, e1,
  542.                              build_tree_list (NULL_TREE, e2)));
  543.         }
  544.       expr = copy_node (expr);
  545.       TREE_TYPE (expr) = reftype;
  546.       return expr;
  547.     }
  548.       if (decl == error_mark_node)
  549.     flags |= LOOKUP_PROTECTED_OK;
  550.       return build_up_reference (reftype, expr, flags);
  551.     }
  552.  
  553.   /* Definitely need to go through a constructor here.  */
  554.   if (TYPE_HAS_CONSTRUCTOR (type))
  555.     {
  556.       tree init = build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), build_tree_list (NULL_TREE, expr), CLASSTYPE_AS_LIST (type), LOOKUP_NORMAL);
  557.       tree rval;
  558.  
  559.       if (init == error_mark_node)
  560.     return error_mark_node;
  561.       rval = build_cplus_new (type, init, 1);
  562.       if (decl == error_mark_node)
  563.     flags |= LOOKUP_PROTECTED_OK;
  564.       return build_up_reference (reftype, rval, flags);
  565.     }
  566.  
  567.   assert (form != OFFSET_TYPE);
  568.  
  569.   error ("cannot convert to a reference type");
  570.  
  571.   return error_mark_node;
  572. }
  573.  
  574. /* We are using a reference VAL for its value. Bash that reference all the
  575.    way down to its lowest form. */
  576. tree
  577. convert_from_reference (val)
  578.      tree val;
  579. {
  580.   tree type = TREE_TYPE (val);
  581.  
  582.   if (TREE_CODE (type) == OFFSET_TYPE)
  583.     type = TREE_TYPE (type);
  584.  if (TREE_CODE (type) == REFERENCE_TYPE)
  585.     {
  586.       tree target_type = TREE_TYPE (type);
  587.  
  588.       /* This can happen if we cast to a reference type.  */
  589.       if (TREE_CODE (val) == ADDR_EXPR)
  590.     {
  591.       val = build1 (NOP_EXPR, build_pointer_type (target_type), val);
  592.       val = build_indirect_ref (val, 0);
  593.       return val;
  594.     }
  595.  
  596.       val = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (target_type), val);
  597.  
  598.       TREE_THIS_VOLATILE (val) = TREE_VOLATILE (target_type);
  599.       TREE_READONLY (val) = TREE_READONLY (target_type);
  600.     }
  601.   return val;
  602. }
  603.  
  604. static tree
  605. convert_to_real (type, expr)
  606.      tree type, expr;
  607. {
  608.   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
  609.   extern int flag_float_store;
  610.  
  611.   if (form == REAL_TYPE)
  612.     return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
  613.           type, expr);
  614.  
  615.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  616.     return build1 (FLOAT_EXPR, type, expr);
  617.  
  618.   assert (form != OFFSET_TYPE);
  619.  
  620.   if (form == POINTER_TYPE)
  621.     error ("pointer value used where a floating point value was expected");
  622.   /* C++: check to see if we can convert this aggregate type
  623.      into the required scalar type.  */
  624.   else if (IS_AGGR_TYPE (TREE_TYPE (expr)))
  625.     {
  626.       tree rval;
  627.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  628.       if (rval)
  629.     return rval;
  630.       else
  631.     error ("aggregate value used where a floating point value was expected");
  632.     }
  633.  
  634.   {
  635.     register tree tem = make_node (REAL_CST);
  636.     TREE_TYPE (tem) = type;
  637.     TREE_REAL_CST (tem) = 0;
  638.     return tem;
  639.   }
  640. }
  641.  
  642. /* The result of this is always supposed to be a newly created tree node
  643.    not in use in any existing structure.  */
  644.  
  645. static tree
  646. convert_to_integer (type, expr)
  647.      tree type, expr;
  648. {
  649.   register tree intype = TREE_TYPE (expr);
  650.   register enum tree_code form = TREE_CODE (intype);
  651.   extern tree build_binary_op_nodefault ();
  652.   extern tree build_unary_op ();
  653.  
  654.   if (form == POINTER_TYPE)
  655.     {
  656.       if (integer_zerop (expr))
  657.     expr = integer_zero_node;
  658.       else
  659.     expr = fold (build1 (CONVERT_EXPR,
  660.                  type_for_size (POINTER_SIZE, 0), expr));
  661.       intype = TREE_TYPE (expr);
  662.       form = TREE_CODE (intype);
  663.       if (intype == type)
  664.     return expr;
  665.     }
  666.  
  667.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  668.     {
  669.       register int outprec = TYPE_PRECISION (type);
  670.       register int inprec = TYPE_PRECISION (intype);
  671.       register enum tree_code ex_form = TREE_CODE (expr);
  672.  
  673.       if (outprec >= inprec)
  674.     return build1 (NOP_EXPR, type, expr);
  675.  
  676. /* Here detect when we can distribute the truncation down past some arithmetic.
  677.    For example, if adding two longs and converting to an int,
  678.    we can equally well convert both to ints and then add.
  679.    For the operations handled here, such truncation distribution
  680.    is always safe.
  681.    It is desirable in these cases:
  682.    1) when truncating down to full-word from a larger size
  683.    2) when truncating takes no work.
  684.    3) when at least one operand of the arithmetic has been extended
  685.    (as by C's default conversions).  In this case we need two conversions
  686.    if we do the arithmetic as already requested, so we might as well
  687.    truncate both and then combine.  Perhaps that way we need only one.
  688.  
  689.    Note that in general we cannot do the arithmetic in a type
  690.    shorter than the desired result of conversion, even if the operands
  691.    are both extended from a shorter type, because they might overflow
  692.    if combined in that type.  The exceptions to this--the times when
  693.    two narrow values can be combined in their narrow type even to
  694.    make a wider result--are handled by "shorten" in build_binary_op.  */
  695.  
  696.       switch (ex_form)
  697.     {
  698.     case RSHIFT_EXPR:
  699.       /* We can pass truncation down through right shifting
  700.          when the shift count is a negative constant.  */
  701.       if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  702.           || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) > 0)
  703.         break;
  704.       goto trunc1;
  705.  
  706.     case LSHIFT_EXPR:
  707.       /* We can pass truncation down through left shifting
  708.          when the shift count is a positive constant.  */
  709.       if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  710.           || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) < 0)
  711.         break;
  712.       /* In this case, shifting is like multiplication.  */
  713.       goto trunc1;
  714.  
  715.     case MAX_EXPR:
  716.     case MIN_EXPR:
  717.     case MULT_EXPR:
  718.       {
  719.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  720.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  721.  
  722.         /* Don't distribute unless the output precision is at least as big
  723.            as the actual inputs.  Otherwise, the comparison of the
  724.            truncated values will be wrong.  */
  725.         if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
  726.         && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
  727.         /* If signedness of arg0 and arg1 don't match,
  728.            we can't necessarily find a type to compare them in.  */
  729.         && (TREE_UNSIGNED (TREE_TYPE (arg0))
  730.             == TREE_UNSIGNED (TREE_TYPE (arg1))))
  731.           goto trunc1;
  732.         break;
  733.       }
  734.  
  735.     case PLUS_EXPR:
  736.     case MINUS_EXPR:
  737.     case BIT_AND_EXPR:
  738.     case BIT_IOR_EXPR:
  739.     case BIT_XOR_EXPR:
  740.     case BIT_ANDTC_EXPR:
  741.     trunc1:
  742.       {
  743.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  744.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  745.  
  746.         if (outprec >= BITS_PER_WORD
  747.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  748.         || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  749.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  750.           {
  751.         /* Do the arithmetic in type TYPEX,
  752.            then convert result to TYPE.  */
  753.         register tree typex = type;
  754.  
  755.         /* Can't do arithmetic in enumeral types
  756.            so use an integer type that will hold the values.  */
  757.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  758.           typex = type_for_size (TYPE_PRECISION (typex),
  759.                      TREE_UNSIGNED (typex));
  760.  
  761.         /* But now perhaps TYPEX is as wide as INPREC.
  762.            In that case, do nothing special here.
  763.            (Otherwise would recurse infinitely in convert.  */
  764.         if (TYPE_PRECISION (typex) != inprec)
  765.           {
  766.             /* Don't do unsigned arithmetic where signed was wanted,
  767.                or vice versa.
  768.                Exception: if the original operands were unsigned
  769.                then can safely do the work as unsigned.
  770.                And we may need to do it as unsigned
  771.                if we truncate to the original size.  */
  772.             typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
  773.                   || TREE_UNSIGNED (TREE_TYPE (arg0)))
  774.                  ? unsigned_type (typex) : signed_type (typex));
  775.             return convert (type,
  776.                     build_binary_op_nodefault (ex_form,
  777.                                    convert (typex, arg0),
  778.                                    convert (typex, arg1),
  779.                                    ex_form));
  780.           }
  781.           }
  782.       }
  783.       break;
  784.  
  785.     case EQ_EXPR:
  786.     case NE_EXPR:
  787.     case GT_EXPR:
  788.     case GE_EXPR:
  789.     case LT_EXPR:
  790.     case LE_EXPR:
  791.     case TRUTH_AND_EXPR:
  792.     case TRUTH_ANDIF_EXPR:
  793.     case TRUTH_OR_EXPR:
  794.     case TRUTH_ORIF_EXPR:
  795.     case TRUTH_NOT_EXPR:
  796.       /* If we want result of comparison converted to a byte,
  797.          we can just regard it as a byte, since it is 0 or 1.  */
  798.       TREE_TYPE (expr) = type;
  799.       return expr;
  800.  
  801.     case NEGATE_EXPR:
  802.     case BIT_NOT_EXPR:
  803.     case ABS_EXPR:
  804.       {
  805.         register tree typex = type;
  806.  
  807.         /* Can't do arithmetic in enumeral types
  808.            so use an integer type that will hold the values.  */
  809.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  810.           typex = type_for_size (TYPE_PRECISION (typex),
  811.                      TREE_UNSIGNED (typex));
  812.  
  813.         /* But now perhaps TYPEX is as wide as INPREC.
  814.            In that case, do nothing special here.
  815.            (Otherwise would recurse infinitely in convert.  */
  816.         if (TYPE_PRECISION (typex) != inprec)
  817.           {
  818.         /* Don't do unsigned arithmetic where signed was wanted,
  819.            or vice versa.  */
  820.         typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  821.              ? unsigned_type (typex) : signed_type (typex));
  822.         return convert (type,
  823.                 build_unary_op (ex_form,
  824.                         convert (typex, TREE_OPERAND (expr, 0)),
  825.                         1));
  826.           }
  827.       }
  828.  
  829.     case NOP_EXPR:
  830.       /* If truncating after truncating, might as well do all at once.
  831.          If truncating after extending, we may get rid of wasted work.  */
  832.       return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  833.  
  834.     case COND_EXPR:
  835.       /* Can treat the two alternative values like the operands
  836.          of an arithmetic expression.  */
  837.       {
  838.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  839.         tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
  840.  
  841.         if (outprec >= BITS_PER_WORD
  842.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  843.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
  844.         || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
  845.           {
  846.         /* Do the arithmetic in type TYPEX,
  847.            then convert result to TYPE.  */
  848.         register tree typex = type;
  849.  
  850.         /* Can't do arithmetic in enumeral types
  851.            so use an integer type that will hold the values.  */
  852.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  853.           typex = type_for_size (TYPE_PRECISION (typex),
  854.                      TREE_UNSIGNED (typex));
  855.  
  856.         /* But now perhaps TYPEX is as wide as INPREC.
  857.            In that case, do nothing special here.
  858.            (Otherwise would recurse infinitely in convert.  */
  859.         if (TYPE_PRECISION (typex) != inprec)
  860.           {
  861.             /* Don't do unsigned arithmetic where signed was wanted,
  862.                or vice versa.  */
  863.             typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  864.                  ? unsigned_type (typex) : signed_type (typex));
  865.             return convert (type,
  866.                     fold (build (COND_EXPR, typex,
  867.                          TREE_OPERAND (expr, 0),
  868.                          convert (typex, arg1),
  869.                          convert (typex, arg2))));
  870.           }
  871.           }
  872.       }
  873.  
  874.     }
  875.  
  876.       return build1 (NOP_EXPR, type, expr);
  877.     }
  878.  
  879.   if (form == REAL_TYPE)
  880.     return build1 (FIX_TRUNC_EXPR, type, expr);
  881.  
  882.   if (form == OFFSET_TYPE)
  883.     error_with_decl (TYPE_NAME (TYPE_OFFSET_BASETYPE (intype)),
  884.              "pointer-to-member expression object not composed with type `%s' object");
  885.   else
  886.     {
  887.       if (IS_AGGR_TYPE (intype))
  888.     {
  889.       tree rval;
  890.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  891.       if (rval) return rval;
  892.     }
  893.  
  894.       error ("aggregate value used where an integer was expected");
  895.     }
  896.  
  897.   {
  898.     register tree tem = build_int_2 (0, 0);
  899.     TREE_TYPE (tem) = type;
  900.     return tem;
  901.   }
  902. }
  903.  
  904. /* See if there is a constructor of type TYPE which will convert
  905.    EXPR.  The reference manual seems to suggest (8.5.6) that we need
  906.    not worry about finding constructors for base classes, then converting
  907.    to the derived class.
  908.  
  909.    MSGP is a pointer to a message that would be an appropriate error
  910.    string.  If MSGP is NULL, then we are not interested in reporting
  911.    errors.  */
  912. tree
  913. convert_to_aggr (type, expr, msgp, protect)
  914.      tree type, expr;
  915.      char **msgp;
  916. {
  917.   tree basetype = TYPE_MAIN_VARIANT (type);
  918.   tree name = DECL_NAME (TYPE_NAME (basetype));
  919.   tree function, fndecl, fntype, parmtypes, parmlist, result;
  920.   tree method_name;
  921.   enum visibility_type visibility;
  922.   int can_be_private, can_be_protected;
  923.  
  924.   if (! TYPE_HAS_CONSTRUCTOR (basetype))
  925.     {
  926.       if (msgp)
  927.     *msgp = "type `%s' does not have a constructor";
  928.       return error_mark_node;
  929.     }
  930.  
  931.   visibility = visibility_public;
  932.   can_be_private = 0;
  933.   can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
  934.  
  935.   parmlist = build_tree_list (NULL_TREE, expr);
  936.   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
  937.  
  938.   if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
  939.     {
  940.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  941.       parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
  942.     }
  943.  
  944.   /* The type of the first argument will be filled in inside the loop.  */
  945.   parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
  946.   parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
  947.  
  948.   method_name = build_decl_overload (IDENTIFIER_POINTER (name), parmtypes, 1);
  949.  
  950.   /* constructors are up front.  */
  951.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  952.   if (TYPE_HAS_DESTRUCTOR (basetype))
  953.     fndecl = TREE_CHAIN (fndecl);
  954.  
  955.   while (fndecl)
  956.     {
  957.       if (DECL_NAME (fndecl) == method_name)
  958.     {
  959.       function = fndecl;
  960.       if (protect)
  961.         {
  962.           if (TREE_PRIVATE (fndecl))
  963.         {
  964.           can_be_private =
  965.             (basetype == current_class_type
  966.              || is_friend (basetype, current_function_decl)
  967.              || purpose_member (basetype, DECL_VISIBILITY (fndecl)));
  968.           if (! can_be_private)
  969.             goto found;
  970.         }
  971.           else if (TREE_PROTECTED (fndecl))
  972.         {
  973.           if (! can_be_protected)
  974.             goto found;
  975.         }
  976.         }
  977.       goto found_and_ok;
  978.     }
  979.       fndecl = TREE_CHAIN (fndecl);
  980.     }
  981.  
  982.   /* No exact conversion was found.  See if an approximate
  983.      one will do.  */
  984.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  985.   if (TYPE_HAS_DESTRUCTOR (basetype))
  986.     fndecl = TREE_CHAIN (fndecl);
  987.  
  988.   {
  989.     int saw_private = 0;
  990.     int saw_protected = 0;
  991.     struct candidate *candidates =
  992.       (struct candidate *) alloca ((list_length (fndecl)+1) * sizeof (struct candidate));
  993.     struct candidate *cp = candidates;
  994.  
  995.     while (fndecl)
  996.       {
  997.     function = fndecl;
  998.     cp->harshness = (unsigned short *)alloca (3 * sizeof (short));
  999.     compute_conversion_costs (fndecl, parmlist, cp, 2);
  1000.     if (cp->evil == 0)
  1001.       {
  1002.         cp->u.field = fndecl;
  1003.         if (protect)
  1004.           {
  1005.         if (TREE_PRIVATE (fndecl))
  1006.           visibility = visibility_private;
  1007.         else if (TREE_PROTECTED (fndecl))
  1008.           visibility = visibility_protected;
  1009.         else
  1010.           visibility = visibility_public;
  1011.           }
  1012.         else
  1013.           visibility = visibility_public;
  1014.  
  1015.         if (visibility == visibility_private
  1016.         ? (basetype == current_class_type
  1017.            || is_friend (basetype, cp->function)
  1018.            || purpose_member (basetype, DECL_VISIBILITY (fndecl)))
  1019.         : visibility == visibility_protected
  1020.         ? (can_be_protected
  1021.            || purpose_member (basetype, DECL_VISIBILITY (fndecl)))
  1022.         : 1)
  1023.           {
  1024.         if (cp->user == 0 && cp->b_or_d == 0
  1025.             && cp->easy <= 1)
  1026.           {
  1027.             goto found_and_ok;
  1028.           }
  1029.         cp++;
  1030.           }
  1031.         else
  1032.           {
  1033.         if (visibility == visibility_private)
  1034.           saw_private = 1;
  1035.         else
  1036.           saw_protected = 1;
  1037.           }
  1038.       }
  1039.     fndecl = TREE_CHAIN (fndecl);
  1040.       }
  1041.     if (cp - candidates)
  1042.       {
  1043.     /* Rank from worst to best.  Then cp will point to best one.
  1044.        Private fields have their bits flipped.  For unsigned
  1045.        numbers, this should make them look very large.
  1046.        If the best alternate has a (signed) negative value,
  1047.        then all we ever saw were private members.  */
  1048.     if (cp - candidates > 1)
  1049.       qsort (candidates,    /* char *base */
  1050.          cp - candidates, /* int nel */
  1051.          sizeof (struct candidate), /* int width */
  1052.          rank_for_overload); /* int (*compar)() */
  1053.  
  1054.     --cp;
  1055.     if (cp->evil > 1)
  1056.       {
  1057.         if (msgp)
  1058.           *msgp = "ambiguous type conversion possible for `%s'";
  1059.         return error_mark_node;
  1060.       }
  1061.  
  1062.     function = cp->function;
  1063.     fndecl = cp->u.field;
  1064.     goto found_and_ok;
  1065.       }
  1066.     else if (msgp)
  1067.       {
  1068.     if (saw_private)
  1069.       if (saw_protected)
  1070.         *msgp = "only private and protected conversions apply";
  1071.       else
  1072.         *msgp = "only private conversions apply";
  1073.     else if (saw_protected)
  1074.       *msgp = "only protected conversions apply";
  1075.       }
  1076.     return error_mark_node;
  1077.   }
  1078.   /* NOTREACHED */
  1079.  
  1080.  not_found:
  1081.   if (msgp) *msgp = "no appropriate conversion to type `%s'";
  1082.   return error_mark_node;
  1083.  found:
  1084.   if (visibility == visibility_private)
  1085.     if (! can_be_private)
  1086.       {
  1087.     if (msgp)
  1088.       *msgp = TREE_PRIVATE (fndecl)
  1089.         ? "conversion to type `%s' is private"
  1090.         : "conversion to type `%s' is from private base class";
  1091.     return error_mark_node;
  1092.       }
  1093.   if (visibility == visibility_protected)
  1094.     if (! can_be_protected)
  1095.       {
  1096.     if (msgp)
  1097.       *msgp = TREE_PRIVATE (fndecl)
  1098.         ? "conversion to type `%s' is protected"
  1099.         : "conversion to type `%s' is from protected base class";
  1100.     return error_mark_node;
  1101.       }
  1102.   function = fndecl;
  1103.  found_and_ok:
  1104.  
  1105.   /* It will convert, but we don't do anything about it yet.  */
  1106.   if (msgp == 0)
  1107.     return NULL_TREE;
  1108.  
  1109.   fntype = TREE_TYPE (function);
  1110.   if (TREE_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
  1111.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  1112.   else
  1113.     function = default_conversion (function);
  1114.  
  1115.   result = build_nt (CALL_EXPR, function,
  1116.              actualparameterlist (NULL_TREE, TYPE_ARG_TYPES (fntype), parmlist, NULL_TREE, LOOKUP_NORMAL),
  1117.              NULL_TREE);
  1118.   TREE_TYPE (result) = TREE_TYPE (fntype);
  1119.   TREE_VOLATILE (result) = 1;
  1120.   TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  1121.   return result;
  1122. }
  1123.  
  1124. /* Call this when we know (for any reason) that expr is
  1125.    not, in fact, zero.  */
  1126. tree
  1127. convert_pointer_to (type, expr)
  1128.      tree type, expr;
  1129. {
  1130.   register tree intype = TREE_TYPE (expr);
  1131.   register enum tree_code form = TREE_CODE (intype);
  1132.   tree ptr_type = build_pointer_type (type);
  1133.   tree rval;
  1134.  
  1135.   if (TYPE_MAIN_VARIANT (ptr_type) == TYPE_MAIN_VARIANT (intype))
  1136.     return expr;
  1137.  
  1138.   if (intype == error_mark_node)
  1139.     return error_mark_node;
  1140.  
  1141.   assert (form == POINTER_TYPE);
  1142.   assert (!integer_zerop (expr));
  1143.  
  1144.   if (IS_AGGR_TYPE (type)
  1145.       && IS_AGGR_TYPE (TREE_TYPE (intype))
  1146.       && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
  1147.     {
  1148.       tree path, basetype;
  1149.       int distance = get_base_distance (type, TYPE_MAIN_VARIANT (TREE_TYPE (intype)), 0, &path);
  1150.  
  1151.       /* This function shouldn't be called with
  1152.      unqualified arguments.  */
  1153.       assert (distance >= 0);
  1154.  
  1155.       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
  1156.     }
  1157.   rval = build1 (NOP_EXPR, ptr_type,
  1158.          TREE_CODE (expr) == NOP_EXPR
  1159.          ? TREE_OPERAND (expr, 0) : expr);
  1160.   TREE_LITERAL (rval) = TREE_LITERAL (expr);
  1161.   return rval;
  1162. }
  1163.  
  1164. /* Same as above, but don't abort if we get an "ambiguous" baseclass.
  1165.    There's only one virtual baseclass we are looking for, and once
  1166.    we find one such virtual baseclass, we have found them all.  */
  1167.  
  1168. tree
  1169. convert_pointer_to_vbase (type, expr)
  1170.      tree type;
  1171.      tree expr;
  1172. {
  1173.   tree intype = TREE_TYPE (TREE_TYPE (expr));
  1174.   int i;
  1175.  
  1176.   for (i = CLASSTYPE_N_BASECLASSES (intype); i > 0; i--)
  1177.     {
  1178.       tree basetype = CLASSTYPE_BASECLASS (intype, i);
  1179.       if (type == basetype)
  1180.     return convert_pointer_to (type, expr);
  1181.       if (value_member (TYPE_MAIN_VARIANT (type),
  1182.             CLASSTYPE_VBASECLASSES (basetype)))
  1183.     return convert_pointer_to_vbase (type, convert_pointer_to (TYPE_MAIN_VARIANT (basetype), expr));
  1184.     }
  1185.   abort ();
  1186. }
  1187.  
  1188. /* Create an expression whose value is that of EXPR,
  1189.    converted to type TYPE.  The TREE_TYPE of the value
  1190.    is always TYPE.  This function implements all reasonable
  1191.    conversions; callers should filter out those that are
  1192.    not permitted by the language being compiled.  */
  1193.  
  1194. tree
  1195. convert (type, expr)
  1196.      tree type, expr;
  1197. {
  1198.   register tree e = expr;
  1199.   register enum tree_code code = TREE_CODE (type);
  1200.  
  1201.   if (type == TREE_TYPE (expr) || TREE_CODE (expr) == ERROR_MARK)
  1202.     return expr;
  1203.   if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  1204.     return error_mark_node;
  1205.   if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
  1206.     {
  1207.       error ("void value not ignored as it ought to be");
  1208.       return error_mark_node;
  1209.     }
  1210.   if (code == VOID_TYPE)
  1211.     {
  1212.       tree rval = build_type_conversion (NOP_EXPR, type, e, 0);
  1213.       /* If we can convert to void type via a type conversion, do so.  */
  1214.       if (rval)
  1215.     return rval;
  1216.       return build1 (CONVERT_EXPR, type, e);
  1217.     }
  1218. #if 0
  1219.   /* This is incorrect.  A truncation can't be stripped this way.
  1220.      Extensions will be stripped by the use of get_unwidened.  */
  1221.   if (TREE_CODE (expr) == NOP_EXPR)
  1222.     return convert (type, TREE_OPERAND (expr, 0));
  1223. #endif
  1224.  
  1225.   /* Just convert to the type of the member.  */
  1226.   if (code == OFFSET_TYPE)
  1227.     {
  1228.       type = TREE_TYPE (type);
  1229.       code = TREE_CODE (type);
  1230.     }
  1231.  
  1232.   /* C++ */
  1233.   if (code == REFERENCE_TYPE)
  1234.     return fold (convert_to_reference (error_mark_node, type, e, -1, LOOKUP_NORMAL));
  1235.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1236.     e = convert_from_reference (e);
  1237.  
  1238.   if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
  1239.     return fold (convert_to_integer (type, e));
  1240.   if (code == POINTER_TYPE)
  1241.     return fold (convert_to_pointer (type, e));
  1242.   if (code == REAL_TYPE)
  1243.     return fold (convert_to_real (type, e));
  1244.  
  1245.   /* New C++ semantics:  since assignment is now based on
  1246.      memberwise copying,  if the rhs type is derived from the
  1247.      lhs type, then we may still do a conversion.  */
  1248.   if (IS_AGGR_TYPE_CODE (code))
  1249.     {
  1250.       tree dtype = TREE_TYPE (e);
  1251.  
  1252.       if (TREE_CODE (dtype) == REFERENCE_TYPE)
  1253.     {
  1254.       e = convert_from_reference (e);
  1255.       dtype = TREE_TYPE (e);
  1256.     }
  1257.       dtype = TYPE_MAIN_VARIANT (dtype);
  1258.  
  1259.       /* Conversion between aggregate types.  New C++ semantics allow
  1260.      objects of derived type to be cast to objects of base type.
  1261.      Old semantics only allowed this bwteen pointers.
  1262.  
  1263.      There may be some ambiguity between using a constructor
  1264.      vs. using a type conversion operator when both apply.  */
  1265.  
  1266.       if (IS_AGGR_TYPE (dtype))
  1267.     {
  1268.       tree basetype;
  1269.  
  1270.       tree conversion = TYPE_HAS_CONVERSION (dtype)
  1271.         ? build_type_conversion (CONVERT_EXPR, type, e, 1) : NULL_TREE;
  1272.  
  1273.       if (TYPE_HAS_CONSTRUCTOR (type))
  1274.         {
  1275.           tree rval = build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), build_tree_list (NULL_TREE, e), CLASSTYPE_AS_LIST (type),
  1276.                          conversion ? LOOKUP_NO_CONVERSION : 0);
  1277.  
  1278.           if (rval != error_mark_node)
  1279.         {
  1280.           if (conversion)
  1281.             {
  1282.               error ("both constructor and type conversion operator apply");
  1283.               return error_mark_node;
  1284.             }
  1285.           /* call to constructor successful.  */
  1286.           rval = build_cplus_new (type, rval, 0);
  1287.           return rval;
  1288.         }
  1289.         }
  1290.       /* Type conversion successful/applies.  */
  1291.       if (conversion)
  1292.         {
  1293.           if (conversion == error_mark_node)
  1294.         error ("ambiguous pointer conversion");
  1295.           return conversion;
  1296.         }
  1297.  
  1298.       /* now try normal C++ assignment semantics.  */
  1299.       basetype = dtype;
  1300.       if (type == basetype
  1301.           || (basetype = get_base_type (type, dtype, 1)))
  1302.         {
  1303.           if (basetype == error_mark_node)
  1304.         return error_mark_node;
  1305.  
  1306. #if 0
  1307.           if (TYPE_VIRTUAL_P (type))
  1308.         warning ("assignment to virtual aggregate type");
  1309. #endif
  1310.           return build (COMPONENT_REF, type, e, TYPE_NAME (basetype));
  1311.         }
  1312.       error ("conversion between incompatible aggregate types requested");
  1313.       return error_mark_node;
  1314.     }
  1315.       /* conversion from non-aggregate to aggregate type requires constructor.  */
  1316.       else if (TYPE_HAS_CONSTRUCTOR (type))
  1317.     {
  1318.       tree rval;
  1319.       tree init = build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), build_tree_list (NULL_TREE, e), CLASSTYPE_AS_LIST (type), LOOKUP_NORMAL);
  1320.       if (init == error_mark_node)
  1321.         {
  1322.           error_with_aggr_type (type, "in conversion to type `%s'");
  1323.           return error_mark_node;
  1324.         }
  1325.       rval = build_cplus_new (type, init, 0);
  1326.       return rval;
  1327.     }
  1328.     }
  1329.  
  1330.   error ("conversion to non-scalar type requested");
  1331.   return error_mark_node;
  1332. }
  1333.  
  1334. /* Like convert, except permit conversions to take place which
  1335.    are not normally allowed due to visibility restrictions
  1336.    (such as conversion from sub-type to private super-type).  */
  1337. tree
  1338. convert_force (type, expr)
  1339.      tree type;
  1340.      tree expr;
  1341. {
  1342.   register tree e = expr;
  1343.   register enum tree_code code = TREE_CODE (type);
  1344.  
  1345.   if (code == REFERENCE_TYPE)
  1346.     return fold (convert_to_reference (0, type, e, -1, 0));
  1347.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1348.     e = convert_from_reference (e);
  1349.  
  1350.   if (code == POINTER_TYPE)
  1351.     return fold (convert_to_pointer_force (type, e));
  1352.   return convert (type, e);
  1353. }
  1354.  
  1355. /* Subroutine of build_type_conversion.  */
  1356. static tree
  1357. build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
  1358.      tree xtype, basetype;
  1359.      tree expr;
  1360.      tree typename;
  1361.      int for_sure;
  1362. {
  1363.   tree first_arg = expr;
  1364.   tree rval;
  1365.   int flags;
  1366.  
  1367.   if (for_sure == 0)
  1368.     {
  1369.       if (! lvalue_p (expr))
  1370.     first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
  1371.       flags = LOOKUP_PROTECT;
  1372.     }
  1373.   else
  1374.     flags = LOOKUP_NORMAL;
  1375.  
  1376.   rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
  1377.   if (rval == error_mark_node)
  1378.     {
  1379.       if (for_sure == 0)
  1380.     return NULL_TREE;
  1381.       return error_mark_node;
  1382.     }
  1383.   if (first_arg != expr)
  1384.     {
  1385.       expr = build_up_reference (build_reference_type (TREE_TYPE (expr)), expr,
  1386.                  LOOKUP_COMPLAIN);
  1387.       TREE_VALUE (TREE_OPERAND (rval, 1)) = build_unary_op (ADDR_EXPR, expr, 0);
  1388.     }
  1389.   if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
  1390.       && TREE_CODE (xtype) != REFERENCE_TYPE)
  1391.     rval = default_conversion (rval);
  1392.   return convert (xtype, rval);
  1393. }
  1394.  
  1395. /* Convert an aggregate EXPR to type XTYPE.  If a conversion
  1396.    exists, return the attempted conversion.  This may
  1397.    return ERROR_MARK_NODE if the conversion is not
  1398.    allowed (references private members, etc).
  1399.    If no conversion exists, NULL_TREE is returned.
  1400.  
  1401.    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
  1402.    to take place immediately.  Otherwise, we build a SAVE_EXPR
  1403.    which can be evaluated if the results are ever needed.
  1404.  
  1405.    If FOR_SURE >= 2, then we only look for exact conversions.
  1406.  
  1407.    TYPE may be a reference type, in which case we first look
  1408.    for something that will convert to a reference type.  If
  1409.    that fails, we will try to look for something of the
  1410.    reference's target type, and then return a reference to that.  */
  1411. tree
  1412. build_type_conversion (code, xtype, expr, for_sure)
  1413.      enum tree_code code;
  1414.      tree xtype, expr;
  1415.      int for_sure;
  1416. {
  1417.   /* C++: check to see if we can convert this aggregate type
  1418.      into the required scalar type.  */
  1419.   tree type, type_default;
  1420.   tree typename = build_typename_overload (xtype), *typenames;
  1421.   int n_variants = 0;
  1422.   tree basetype, save_basetype;
  1423.   tree rval;
  1424.   int exact_conversion = for_sure >= 2;
  1425.   for_sure &= 1;
  1426.  
  1427.   if (expr == error_mark_node)
  1428.     return error_mark_node;
  1429.  
  1430.   basetype = TREE_TYPE (expr);
  1431.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1432.     basetype = TREE_TYPE (basetype);
  1433.  
  1434.   basetype = TYPE_MAIN_VARIANT (basetype);
  1435.   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
  1436.     return 0;
  1437.  
  1438.   if (TREE_CODE (xtype) == POINTER_TYPE
  1439.       || TREE_CODE (xtype) == REFERENCE_TYPE)
  1440.     {
  1441.       /* Prepare to match a variant of this type.  */
  1442.       type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1443.       for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type))
  1444.     n_variants++;
  1445.       typenames = (tree *)alloca (n_variants * sizeof (tree));
  1446.       for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1447.        type; n_variants++, type = TYPE_NEXT_VARIANT (type))
  1448.     {
  1449.       if (type == TREE_TYPE (xtype))
  1450.         typenames[n_variants] = typename;
  1451.       else if (TREE_CODE (xtype) == POINTER_TYPE)
  1452.         typenames[n_variants] = build_typename_overload (build_pointer_type (type));
  1453.       else
  1454.         typenames[n_variants] = build_typename_overload (build_reference_type (type));
  1455.     }
  1456.     }
  1457.  
  1458.   save_basetype = basetype;
  1459.   type = xtype;
  1460.  
  1461.   while (TYPE_HAS_CONVERSION (basetype))
  1462.     {
  1463.       int i;
  1464.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1465.     return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1466.       for (i = 0; i < n_variants; i++)
  1467.     if (typenames[i] != typename
  1468.         && lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typenames[i], 0))
  1469.       return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure);
  1470.  
  1471.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1472.     basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1473.       else break;
  1474.     }
  1475.  
  1476.   if (TREE_CODE (type) == REFERENCE_TYPE)
  1477.     {
  1478.       tree first_arg = expr;
  1479.       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  1480.       basetype = save_basetype;
  1481.  
  1482.       /* May need to build a temporary for this.  */
  1483.       while (TYPE_HAS_CONVERSION (basetype))
  1484.     {
  1485.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1486.         {
  1487.           int flags;
  1488.  
  1489.           if (for_sure == 0)
  1490.         {
  1491.           if (! lvalue_p (expr))
  1492.             first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
  1493.           flags = LOOKUP_PROTECT;
  1494.         }
  1495.           else
  1496.         flags = LOOKUP_NORMAL;
  1497.           rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
  1498.           if (rval == error_mark_node)
  1499.         {
  1500.           if (for_sure == 0)
  1501.             return NULL_TREE;
  1502.           return error_mark_node;
  1503.         }
  1504.           TREE_VALUE (TREE_OPERAND (rval, 1)) = expr;
  1505.  
  1506.           if (IS_AGGR_TYPE (type))
  1507.         {
  1508.           tree init = build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), build_tree_list (NULL_TREE, rval), NULL_TREE, LOOKUP_NORMAL);
  1509.           tree temp = build_cplus_new (type, init, 1);
  1510.           return build_up_reference (TYPE_REFERENCE_TO (type), temp,
  1511.                          LOOKUP_COMPLAIN);
  1512.         }
  1513.           return convert (xtype, rval);
  1514.         }
  1515.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1516.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1517.       else break;
  1518.     }
  1519.       /* No free conversions for reference types, right?.  */
  1520.       return NULL_TREE;
  1521.     }
  1522.  
  1523.   if (exact_conversion)
  1524.     return NULL_TREE;
  1525.  
  1526.   /* No perfect match found, try default.  */
  1527.   if (code == CONVERT_EXPR && TREE_CODE (type) == POINTER_TYPE)
  1528.     type_default = ptr_type_node;
  1529.   else if (type == void_type_node)
  1530.     return NULL_TREE;
  1531.   else
  1532.     {
  1533.       extern tree default_conversion ();
  1534.       tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node));
  1535.       if (tmp == error_mark_node)
  1536.     return NULL_TREE;
  1537.       type_default = TREE_TYPE (tmp);
  1538.     }
  1539.  
  1540.   basetype = save_basetype;
  1541.  
  1542.   if (type_default != type)
  1543.     {
  1544.       type = type_default;
  1545.       typename = build_typename_overload (type);
  1546.  
  1547.       while (TYPE_HAS_CONVERSION (basetype))
  1548.     {
  1549.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1550.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1551.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1552.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1553.       else break;
  1554.     }
  1555.     }
  1556.  
  1557.  try_pointer:
  1558.  
  1559.   if (type == ptr_type_node)
  1560.     {
  1561.       /* Try converting to some other pointer type
  1562.      with which void* is compatible, or in situations
  1563.      in which void* is appropriate (such as &&,||, and !).  */
  1564.  
  1565.       while (TYPE_HAS_CONVERSION (basetype))
  1566.     {
  1567.       if (CLASSTYPE_CONVERSION (basetype, ptr_conv) != 0)
  1568.         {
  1569.           if (CLASSTYPE_CONVERSION (basetype, ptr_conv) == error_mark_node)
  1570.         return error_mark_node;
  1571.           typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONVERSION (basetype, ptr_conv));
  1572.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1573.         }
  1574.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1575.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1576.       else break;
  1577.     }
  1578.     }
  1579.   if (TREE_CODE (type) == POINTER_TYPE
  1580.       && TREE_READONLY (TREE_TYPE (type))
  1581.       && TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
  1582.     {
  1583.       /* Try converting to some other pointer type
  1584.      with which const void* is compatible.  */
  1585.  
  1586.       while (TYPE_HAS_CONVERSION (basetype))
  1587.     {
  1588.       if (CLASSTYPE_CONVERSION (basetype, constptr_conv) != 0)
  1589.         {
  1590.           if (CLASSTYPE_CONVERSION (basetype, constptr_conv) == error_mark_node)
  1591.         return error_mark_node;
  1592.           typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONVERSION (basetype, constptr_conv));
  1593.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1594.         }
  1595.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1596.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1597.       else break;
  1598.     }
  1599.     }
  1600.   /* Use the longer or shorter conversion that is appropriate.  */
  1601.   if (TREE_CODE (type) == INTEGER_TYPE
  1602.       && TYPE_HAS_INT_CONVERSION (basetype)
  1603.       && CLASSTYPE_CONVERSION (basetype, int_conv) != error_mark_node)
  1604.     {
  1605.       typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONVERSION (basetype, int_conv));
  1606.       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1607.     }
  1608.   if (TREE_CODE (type) == REAL_TYPE
  1609.       && TYPE_HAS_REAL_CONVERSION (basetype)
  1610.       && CLASSTYPE_CONVERSION (basetype, real_conv) != error_mark_node)
  1611.     {
  1612.       typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONVERSION (basetype, real_conv));
  1613.       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1614.     }
  1615.  
  1616.   /* THIS IS A KLUDGE.  */
  1617.   if (TREE_CODE (type) != POINTER_TYPE
  1618.       && (code == TRUTH_ANDIF_EXPR
  1619.       || code == TRUTH_ORIF_EXPR
  1620.       || code == TRUTH_NOT_EXPR))
  1621.     {
  1622.       /* Here's when we can convert to a pointer.  */
  1623.       type = ptr_type_node;
  1624.       goto try_pointer;
  1625.     }
  1626.  
  1627.   /* THESE ARE TOTAL KLUDGES.  */
  1628.   /* Default promotion yields no new alternatives, try
  1629.      conversions which are anti-default, such as
  1630.  
  1631.      double -> float or int -> unsigned or unsigned -> long
  1632.  
  1633.      */
  1634.   if (type_default == type)
  1635.     {
  1636.       int not_again = 0;
  1637.  
  1638.       if (type == double_type_node)
  1639.     typename = build_typename_overload (float_type_node);
  1640.       else if (type == integer_type_node)
  1641.     typename = build_typename_overload (unsigned_type_node);
  1642.       else if (type == unsigned_type_node)
  1643.     typename = build_typename_overload (long_integer_type_node);
  1644.  
  1645.     again:
  1646.       basetype = save_basetype;
  1647.       while (TYPE_HAS_CONVERSION (basetype))
  1648.     {
  1649.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1650.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1651.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1652.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1653.       else break;
  1654.     }
  1655.       if (! not_again && type == integer_type_node)
  1656.     {
  1657.       typename = build_typename_overload (long_integer_type_node);
  1658.       not_again = 1;
  1659.       goto again;
  1660.     }
  1661.     }
  1662.  
  1663.   /* Now, try C promotions...
  1664.  
  1665.      float -> int
  1666.      int -> float, void *
  1667.      void * -> int
  1668.  
  1669.      Truthvalue conversions let us try to convert
  1670.      to pointer if we were going for int, and to int
  1671.      if we were looking for pointer.  */
  1672.  
  1673.     basetype = save_basetype;
  1674.     if (TREE_CODE (type) == REAL_TYPE
  1675.     || (TREE_CODE (type) == POINTER_TYPE
  1676.         && (code == TRUTH_ANDIF_EXPR
  1677.         || code == TRUTH_ORIF_EXPR
  1678.         || code == TRUTH_NOT_EXPR)))
  1679.       type = integer_type_node;
  1680.     else if (TREE_CODE (type) == INTEGER_TYPE)
  1681.       if (TYPE_HAS_REAL_CONVERSION (basetype))
  1682.     type = double_type_node;
  1683.       else
  1684.     return NULL_TREE;
  1685.     else
  1686.       return NULL_TREE;
  1687.  
  1688.     typename = build_typename_overload (type);
  1689.     while (TYPE_HAS_CONVERSION (basetype))
  1690.       {
  1691.     if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1692.       {
  1693.         rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1694.         return rval;
  1695.       }
  1696.     if (CLASSTYPE_N_BASECLASSES (basetype))
  1697.       basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1698.     else
  1699.       break;
  1700.       }
  1701.  
  1702.   return NULL_TREE;
  1703. }
  1704.  
  1705. /* Must convert two aggregate types to non-aggregate type.
  1706.    Attempts to find a non-ambiguous, "best" type conversion.
  1707.  
  1708.    Return 1 on success, 0 on failure.
  1709.  
  1710.    @@ What are the real semantics of this supposed to be??? */
  1711. int
  1712. build_default_binary_type_conversion (code, arg1, arg2)
  1713.      enum tree_code code;
  1714.      tree *arg1, *arg2;
  1715. {
  1716.   tree type1 = TREE_TYPE (*arg1);
  1717.   tree type2 = TREE_TYPE (*arg2);
  1718.   char *name1, *name2;
  1719.  
  1720.   if (TREE_CODE (type1) == REFERENCE_TYPE)
  1721.     type1 = TREE_TYPE (type1);
  1722.   if (TREE_CODE (type2) == REFERENCE_TYPE)
  1723.     type2 = TREE_TYPE (type2);
  1724.  
  1725.   if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
  1726.     {
  1727.       tree decl = typedecl_for_tag (type1);
  1728.       if (decl)
  1729.     error ("type conversion nonexistant for type `%s'",
  1730.            IDENTIFIER_POINTER (DECL_NAME (decl)));
  1731.       else
  1732.     error ("type conversion nonexistant for non-C++ type");
  1733.       return 0;
  1734.     }
  1735.   if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
  1736.     {
  1737.       tree decl = typedecl_for_tag (type2);
  1738.       if (decl)
  1739.     error ("type conversion nonexistant for type `%s'",
  1740.            IDENTIFIER_POINTER (decl));
  1741.       else
  1742.     error ("type conversion nonexistant for non-C++ type");
  1743.       return 0;
  1744.     }
  1745.  
  1746.   name1 = TYPE_NAME_STRING (type1);
  1747.   name2 = TYPE_NAME_STRING (type2);
  1748.  
  1749.   if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1))
  1750.     {
  1751.       if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
  1752.     error ("type conversion required for binary operation on types `%s' and `%s'",
  1753.            name1, name2);
  1754.       else
  1755.     error ("type conversion required for type `%s'", name1);
  1756.       return 0;
  1757.     }
  1758.   else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
  1759.     {
  1760.       error ("type conversion required for type `%s'", name2);
  1761.       return 0;
  1762.     }
  1763.  
  1764.   if (TYPE_HAS_INT_CONVERSION (type1) && TYPE_HAS_REAL_CONVERSION (type1))
  1765.     warning ("ambiguous type conversion for type `%s', defaulting to int", name1);
  1766.   if (TYPE_HAS_INT_CONVERSION (type1))
  1767.     {
  1768.       *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
  1769.       *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
  1770.     }
  1771.   else if (TYPE_HAS_REAL_CONVERSION (type1))
  1772.     {
  1773.       *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
  1774.       *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
  1775.     }
  1776.   else
  1777.     {
  1778.       *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
  1779.       if (*arg1 == error_mark_node)
  1780.     error ("ambiguous pointer conversion");
  1781.       *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
  1782.       if (*arg1 != error_mark_node && *arg2 == error_mark_node)
  1783.     error ("ambiguous pointer conversion");
  1784.     }
  1785.   if (*arg1 == 0)
  1786.     {
  1787.       if (*arg2 == 0 && type1 != type2)
  1788.     error ("default type conversion for types `%s' and `%s' failed",
  1789.            name1, name2);
  1790.       else
  1791.     error ("default type conversion for type `%s' failed", name1);
  1792.       return 0;
  1793.     }
  1794.   else if (*arg2 == 0)
  1795.     {
  1796.       error ("default type conversion for type `%s' failed", name2);
  1797.       return 0;
  1798.     }
  1799.   return 1;
  1800. }
  1801.  
  1802. /* Must convert two aggregate types to non-aggregate type.
  1803.    Attempts to find a non-ambiguous, "best" type conversion.
  1804.  
  1805.    Return 1 on success, 0 on failure.
  1806.  
  1807.    The type of the argument is expected to be of aggregate type here.
  1808.  
  1809.    @@ What are the real semantics of this supposed to be??? */
  1810. int
  1811. build_default_unary_type_conversion (code, arg)
  1812.      enum tree_code code;
  1813.      tree *arg;
  1814. {
  1815.   tree type = TREE_TYPE (*arg);
  1816.   tree id = TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
  1817.     ? DECL_NAME (TYPE_NAME (type)) : TYPE_NAME (type);
  1818.   char *name = IDENTIFIER_POINTER (id);
  1819.  
  1820.   if (! TYPE_HAS_CONVERSION (type))
  1821.     {
  1822.       error ("type conversion required for type `%s'", name);
  1823.       return 0;
  1824.     }
  1825.  
  1826.   if (TYPE_HAS_INT_CONVERSION (type) && TYPE_HAS_REAL_CONVERSION (type))
  1827.     warning ("ambiguous type conversion for type `%s', defaulting to int", name);
  1828.   if (TYPE_HAS_INT_CONVERSION (type))
  1829.     *arg = build_type_conversion (code, integer_type_node, *arg, 1);
  1830.   else if (TYPE_HAS_REAL_CONVERSION (type))
  1831.     *arg = build_type_conversion (code, double_type_node, *arg, 1);
  1832.   else
  1833.     {
  1834.       *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
  1835.       if (*arg == error_mark_node)
  1836.     error ("ambiguous pointer conversion");
  1837.     }
  1838.   if (*arg == 0)
  1839.     {
  1840.       error ("default type conversion for type `%s' failed", name);
  1841.       return 0;
  1842.     }
  1843.   return 1;
  1844. }
  1845.